<template>
  <div id="home">
    <nav-bar class="home-nav">
      <div slot="center">购物街</div>
    </nav-bar>
    <tab-control :titles="['流行', '新款', '精选']"
                 @tabClick="tabClick"
                 ref="tabControl1"
                 class="tab-control"
                 v-show="isTabFixed"
    />

    <scroll class="content"
            ref="scroll"
            :probe-type="3"
            @scroll="contentScroll"
            :pull-up-load="true"
            @pullingUp="loadMore"
    >
      <home-swiper :banners="banners" @swiperImageLoad="swiperImageLoad"/>
      <recommend-view :recommends="recommends"/>
      <feature/>
      <tab-control :titles="['流行', '新款', '精选']"
                   @tabClick="tabClick"
                   ref="tabControl2"
      />
      <goods-list :goods="showGoods"/>
    </scroll>

    <back-top @click.native="backClick" v-show="isShowBackTop"/>
  </div>
</template>

<script>

//home自身的组件
import HomeSwiper from "./childComps/HomeSwiper";
import RecommendView from "./childComps/RecommendView";
import Feature from "./childComps/Feature";

//公共的组件
import NavBar from "components/common/navbar/NavBar";
import TabControl from "components/content/tabControl/TabControl";
import GoodsList from "components/content/goods/GoodsList";
import Scroll from "components/common/scroll/Scroll";

//
import {getHomeMultidata, getHomeGoods} from "network/home";
import {debounce} from "common/utils";
import {backTopMixin} from  'common/mixin';


export default {
  name: "Home",
  components: {
    HomeSwiper,
    RecommendView,
    Feature,
    NavBar,
    TabControl,
    GoodsList,
    Scroll,
  },
  data() {
    return {
      banners: [],
      recommends: [],
      goods: {
        'pop': {page: 0, list: []},
        'new': {page: 0, list: []},
        'sell': {page: 0, list: []},
      },
      currentType: 'pop',
      tabOffsetTop: 0,
      isTabFixed: false,
      saveY: 0
    }
  },
  mixins: [backTopMixin],
  computed: {
    showGoods() {
      return this.goods[this.currentType].list
    }
  },
  destroyed() {
    console.log('home销毁')
  },
  //激活时触发
  activated() {
    this.$refs.scroll.scrollTo(0,this.saveY)
    this.$refs.scroll.refresh()
  },
  //失活时触发
  deactivated() {
    //1.保存Y值
    this.saveY = this.$refs.scroll.getScrollY()

    //2.取消全局事件的监听
    this.$bus.$off('itemImageLoad')
  },
  created() {
    //1.请求多个数据 轮播 推荐
    this.getHomeMultidata()

    //2.请求商品数据
    this.getHomeGoods('pop')
    this.getHomeGoods('new')
    this.getHomeGoods('sell')

  },
  mounted() {
    //1.图片加载完成的事件监听
    const refresh = debounce(this.$refs.scroll.refresh, 500)
    this.$bus.$on('itemImageLoad', () => {
      //调用refresh 重新计算可滚动高度
      // this.$refs.scroll.refresh()
      refresh()

    });
  },
  methods: {
    /*
     * 事件监听相关方法
     * */
    //currentType的切换
    tabClick(index) {
      switch (index) {
        case 0:
          this.currentType = 'pop'
          break;
        case 1:
          this.currentType = 'new'
          break;
        case 2:
          this.currentType = 'sell'
          break;
      }
      // 让TabControl的currentIndex 保持一致
      this.$refs.tabControl1.currentIndex = index;
      this.$refs.tabControl2.currentIndex = index;
    },
    //控制backTop的显示与隐藏
    contentScroll(position) {
      //1.判断BackTop 是否显示
      this.isShowBackTop = -position.y > 1000

      //2.决定tabControl是否吸顶  fiexd
      this.isTabFixed = -position.y > this.tabOffsetTop
    },
    //上拉加载更多
    loadMore() {
      //调用getHomeGoods
      this.getHomeGoods(this.currentType);
    },
    //轮播图图片加载完执行
    swiperImageLoad() {
      //  2.获取tabControl的offsetTop
      this.tabOffsetTop = this.$refs.tabControl2.$el.offsetTop
    },

    /*
     * 网络请求相关方法
     * */
    getHomeMultidata() {
      getHomeMultidata().then(res => {
        // console.log(res);
        this.banners = res.data.banner.list;
        this.recommends = res.data.recommend.list;
      });
    },
    getHomeGoods(type) {
      const page = this.goods[type].page + 1;
      getHomeGoods(type, page).then(res => {
        this.goods[type].list.push(...res.data.list);
        this.goods[type].page += 1;
        //每次触发 pullingUp 钩子后，调用 finishPullUp() 准备好下一次的 pullingUp 钩子。
        this.$refs.scroll.finishPullUp()
      })
    },

  }

}
</script>

<style scoped>
#home {
  position: relative;
  padding-top: 44px;
  height: 100vh;
}

.home-nav {
  background-color: var(--color-tint);
  color: #fff;
  /*在使用游览器原生滚动时， 为了让让导航不跟随一起滚动*/
  top: 0;
  position: fixed;
  left: 0;
  right: 0;
  z-index: 19;
}

.content {
  position: absolute;
  /*overflow: hidden;*/
  top: 44px;
  bottom: 49px;
  left: 0;
  right: 0;
}

/*.content {
  height: calc(100% - 49px);
  overflow: hidden;
}*/

.tab-control {
  position: relative;
  z-index: 9;
}
</style>
